<?php
error_reporting(E_ALL);
class taobaoApi {
	const appKey = '12013685';
	const secretCode = 'sandboxcbde0f1b04b37728cb9813c97';
	const apiUrl = "http://gw.api.tbsandbox.com/router/rest?";
	const taokePid = "sandbox_c_1";
	const itemOrderDesc = "desc";
	const itemOrderAsc = "asc";
	const itemOrderPrice = "price";
	const itemOrderDelistTime = "delist_time";
	const itemOrderSellerCredit = "seller_credit";
	
	public function __construct() {
		
	//TODO - Insert your code here
	}
	
	/**
	 * 签名函数 
	 */
	private function createSign ($paramArr) 
	{ 
	    $appSecret = self::secretCode;
		$sign = $appSecret;
	    ksort($paramArr);
	    foreach ($paramArr as $key => $val) 
	    { 
	       if ($key !='' && $val !='') 
	       { 
	           $sign .= $key.$val; 
	       } 
	    }
		$sign .= $appSecret;
	    $sign = strtoupper(md5($sign)); 
	    return $sign;
	} 
	/**
	 * 组参函数
	 */
	private function createStrParam ($paramArr) 
	{ 
	    $strParam = ''; 
		foreach ($paramArr as $key => $val) 
	    { 
	       if ($key != '' && $val !='') 
	       { 
	           $strParam .= $key.'='.urlencode($val).'&'; 
	       } 
	    } 
	    return $strParam; 
	}
	
	public function getProvinces()
	{
		//参数数组 
		$paramArr = array( 
		    'app_key' => self::appKey,
		    'method' => 'taobao.areas.get', 
		    'format' => 'json', 
		    'v' => '2.0', 
			'timestamp' => date('Y-m-d H:i:s'), 
			'fields' => 'area_id,area_type,area_name,parent_id,zip'
		);
		//生成签名 
		$sign = $this->createSign($paramArr); 
		//组织参数 
		$strParam = $this->createStrParam($paramArr); 
		$strParam .= 'sign='.$sign; 
		//访问服务 
		$url = self::apiUrl.$strParam; 
		$retStr =  file_get_contents ($url); 
		$allArea = json_decode($retStr,true);
		$provinces = array();
		foreach ($allArea['rsp']['areas'] as $area)
		{
			if ($area['area_type']=="2")
			{
				$provinces[] = $area;
			}
		}
		return $provinces;
	}
	
	/**
	 * 获取淘宝的类目列表
	 * @param $parentCid 类目的父id
	 * @return array
	 */
	public function getCats($parentCid='0')
	{
		//参数数组 
		$paramArr = array( 
		    'app_key' => self::appKey,
		    'method' => 'taobao.itemcats.get', 
		    'format' => 'json', 
		    'v' => '2.0', 
			'timestamp' => date('Y-m-d H:i:s'), 
			'fields'=> 'cid,parent_cid,name,is_parent,status,sort_order',
		    'parent_cid' => $parentCid
		);
//		print_r($paramArr);
		//生成签名 
		$sign = $this->createSign($paramArr); 
		//组织参数 
		$strParam = $this->createStrParam($paramArr); 
		$strParam .= 'sign='.$sign; 
		//访问服务 
		$url = self::apiUrl.$strParam; 
		$retStr =  file_get_contents ($url); 
		$tmp =  json_decode($retStr,true);
		return $tmp['rsp']['item_cats'];
	}
	
//	public function getCatInfo($catId)
//	{
//		//参数数组 
//		$paramArr = array( 
//		    'app_key' => self::appKey,
//		    'method' => 'taobao.itempropvalues.get', 
//		    'format' => 'json', 
//		    'v' => '1.0', 
//			'timestamp' => date('Y-m-d H:i:s'), 
//		    'fields ' => 'cid,pid,prop_name,vid,name,name_alias,status,sort_order',
//			'cid' => $catId,
//			'datetime' => '1970-1-1 00:00:00' 
//		
//		);
//		//生成签名 
//		$sign = $this->createSign($paramArr); 
//		//组织参数 
//		$strParam = $this->createStrParam($paramArr); 
//		$strParam .= 'sign='.$sign; 
//		//访问服务 
//		$url = self::apiUrl.$strParam; 
//		$retStr =  file_get_contents ($url); 
//		$tmp =  json_decode($retStr,true);
//		return $tmp['rsp'];
//		
//	}
	
	/**
	 * 获取商品搜索结果
	 * @param $keyWord
	 * @param $catID
	 * @param $startPrice
	 * @param $endPrice
	 * @param $pageNo
	 * @param $pageSize
	 * @param $orderBy
	 * @param $city
	 * @return unknown_type
	 */
	public function seaTaobaoPruduct($keyWord,$catID='0',$startPrice=0,$endPrice=100000000,$pageNo=1,
					$pageSize=40,$orderBy="",$state="")
	{
		//参数数组 
		$paramArr = array( 
		    'app_key' => self::appKey,
			'v' => '2.0',
		    'method' => 'taobao.items.search', 
			'sign_method'=>'md5', 
		    'format' => 'json', 
			'timestamp' => date('Y-m-d H:i:s'), 
			'fields' => 'iid,title,nick,pic_path,cid,price,type,delist_time,post_fee', 
			'cid' => $catID,
		    'q' => $keyWord,
			'start_price' => $startPrice,
			'end_price' => $endPrice,
			'page_no' => $pageNo,
			'page_size' => $pageSize,
			'order_by' => $orderBy,
			'location.state' => $state
		);
		//生成签名 
		$sign = $this->createSign($paramArr); 
		//组织参数 
		$strParam = $this->createStrParam($paramArr); 
		$strParam .= 'sign='.$sign; 
		//访问服务 
		$url = self::apiUrl.$strParam; 
		$retStr =  file_get_contents ($url); 
		$tmp =  json_decode($retStr,true);
		return $tmp['rsp'];
	}
	
	/**
	 * 获取商品的详细信息
	 * @param $iid 宝贝id
	 * @param $nick 掌柜名称
	 * @return unknown_type
	 */
	public function getItemInfo($iid,$nick)
	{
		//参数数组 
		$paramArr = array( 
		    'app_key' => self::appKey,
		    'method' => 'taobao.item.get', 
		    'format' => 'json', 
		    'v' => '1.0', 
			'timestamp' => date('Y-m-d H:i:s'), 
			'fields' => 'iid,detail_url,num_iid,title,nick,type,cid,desc,pic_path,num,valid_thru,location,price,post_fee,express_fee,ems_fee,freight_payer,has_invoice,has_warranty,itemimg,propimg,', 
			'nick' => $nick,
			'iid' => $iid
		);
		//生成签名 
		$sign = $this->createSign($paramArr); 
		//组织参数 
		$strParam = $this->createStrParam($paramArr); 
		$strParam .= 'sign='.$sign; 
		//访问服务 
		$url = self::apiUrl.$strParam; 
		$retStr =  file_get_contents ($url); 
		$tmp =  json_decode($retStr,true);
//		print_r($tmp);
		return $tmp['rsp']['items'];
	}
	
	/**
	 * 
	 * @param $rateType
	 * @param $role
	 * @param $result
	 * @return unknown_type
	 */
	public function getTraderates($nick)
	{
		$paramArr = array( 
		    'app_key' => self::appKey,
		    'method' => 'taobao.user.get', 
		    'format' => 'json', 
		    'v' => '1.0', 
			'timestamp' => date('Y-m-d H:i:s'), 
			'fields' => 'buyer_credit', 
			'nick' => $nick,
		);
		//生成签名 
		$sign = $this->createSign($paramArr); 
		//组织参数 
		$strParam = $this->createStrParam($paramArr); 
		$strParam .= 'sign='.$sign; 
		//访问服务 
		$url = self::apiUrl.$strParam; 
		$retStr =  file_get_contents ($url); 
		$tmp =  json_decode($retStr,true);
		return $tmp['rsp']['users'][0]['buyer_credit'];
	}
	
	/**
	 * 获取商品的淘客信息
	 * @param $iid
	 * @param $nick
	 * @return unknown_type
	 */
	public function getTaokeProductInfo($iid,$nick=self::taokePid)
	{
		$paramArr = array( 
		    'app_key' => self::appKey,
		    'method' => 'taobao.taobaoke.items.convert', 
		    'format' => 'json', 
		    'v' => '1.0', 
			'timestamp' => date('Y-m-d H:i:s'), 
			'fields' => 'iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume', 
			'iids' => $iid,
			'nick' =>$nick
		);
		//生成签名 
		$sign = $this->createSign($paramArr); 
		//组织参数 
		$strParam = $this->createStrParam($paramArr); 
		$strParam .= 'sign='.$sign; 
		//访问服务 
		$url = self::apiUrl.$strParam; 
		$retStr =  file_get_contents ($url); 
		$tmp =  json_decode($retStr,true);
		return $tmp['rsp']['taobaokeItems'];
	}
	
	/**
	 * 获取店铺的淘客信息
	 * @param $sid
	 * @param $nick
	 * @return unknown_type
	 */
	public function getTaokeShopInfo($sid,$nick=self::taokePid)
	{
		$paramArr = array( 
		    'app_key' => self::appKey,
		    'method' => 'taobao.taobaoke.shops.convert', 
		    'format' => 'json', 
		    'v' => '1.0', 
			'timestamp' => date('Y-m-d H:i:s'), 
			'fields' => 'user_id,shop_title,click_url,shop_commission.rate', 
			'sids' => $sid,
			'nick' =>$nick
		);
		//生成签名 
		$sign = $this->createSign($paramArr); 
		//组织参数 
		$strParam = $this->createStrParam($paramArr); 
		$strParam .= 'sign='.$sign; 
		//访问服务 
		$url = self::apiUrl.$strParam; 
		$retStr =  file_get_contents ($url); 
		$tmp =  json_decode($retStr,true);
//		print_r($tmp);
		return $tmp['rsp']['taobaokeShops'];
	}
	
	public function getShopInfo($nick)
	{
		$paramArr = array( 
		    'app_key' => self::appKey,
		    'method' => 'taobao.shop.get', 
		    'format' => 'json', 
		    'v' => '1.0', 
			'timestamp' => date('Y-m-d H:i:s'), 
			'fields' => 'sid,cid,title,nick,desc,bulletin,pic_path,created,modified', 
			'nick' =>$nick
		);
		//生成签名 
		$sign = $this->createSign($paramArr); 
		//组织参数 
		$strParam = $this->createStrParam($paramArr); 
		$strParam .= 'sign='.$sign; 
		//访问服务 
		$url = self::apiUrl.$strParam; 
		$retStr =  file_get_contents ($url); 
		$tmp =  json_decode($retStr,true);
		return $tmp['rsp']['shops'];
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
//END CLASS
}

?>